package com.fudan.backend.service.impl;

import com.fudan.backend.dao.AccountDao;
import com.fudan.backend.dao.UserDao;
import com.fudan.backend.pojo.Account;
import com.fudan.backend.pojo.User;
import com.fudan.backend.service.UserService;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.util.Snowflake;
import com.fudan.backend.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private Snowflake snowflake;

    @Autowired
    private StringUtil stringUtil;


    @Override
    public RetCode login(String username, String password) {
        User user = userDao.selectUserByUserName(username);
        if(user == null){
            return RetCode.USERNAME_ERROR;
        }
        if(!user.getPassword().equals(password)){
            return RetCode.PASSWORD_ERROR;
        }
        return RetCode.USER_SUCCESS;
    }

    @Override
    public User getUserByUserId(String userId) {
        return userDao.selectUserByUserId(userId);
    }

    @Override
    public PageInfo<User> getAllUserByPage(int current, int pagesize) {
        PageHelper.startPage(current, pagesize);
        List<User> list = userDao.getAllUser();
        return new PageInfo<User>(list, 5);
    }
    @Transactional
    @Override
    public RetCode signUp(User user) {
        RetCode ret = null;
        if(userDao.selectUserByUserName(user.getUsername()) != null){
            ret = RetCode.USERNAME_EXISTS;
        }
        else if(userDao.selectUserByPhoneNumber(user.getPhoneNumber()) != null){
            ret = RetCode.PHONE_NUM_EXISTS;
        }
        else if(userDao.selectUserByidCardNumber(user.getIdCardNumber()) != null){
            ret = RetCode.ID_CARD_NUM_EXISTS;
        }
        else if(userDao.selectUserByEmail(user.getEmail()) != null){
            ret = RetCode.EMAIL_EXISTS;
        }
        if(ret != null){
            return ret;
        }
        user.setUserId((snowflake.nextId()).toString());
        String accountId =(snowflake.nextId()).toString();
        user.setAccountId(accountId);
        userDao.insertUser(user);
        accountDao.insertAccount(new Account(accountId));
        return RetCode.USER_SUCCESS;
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.selectUserByUserName(username);
    }

    @Override
    public RetCode checkUserFormat(User user) {
        RetCode ret = null;
        if(user == null){
            ret = RetCode.NULL_USER;
        }
        else if(!stringUtil.checkUserNameFormat(user.getUsername())){
            ret = RetCode.USERNAME_FORMAT_ERROR;
        }
        else if(!stringUtil.checkPhoneNumberFormat(user.getPhoneNumber())){
            ret = RetCode.PHONE_NUM_FORMAT_ERROR;
        }
        else if(!stringUtil.checkIdCardNumberFormat(user.getIdCardNumber())){
            ret = RetCode.ID_CARD_NUM_FORMAT_ERROR;
        }
        else if(!stringUtil.checkEmailFomat(user.getEmail())){
            ret = RetCode.EMAIL_FORMAT_ERROR;
        }
        else{
            ret = RetCode.USER_SUCCESS;
        }
        return ret;
    }

    @Override
    public RetCode changePassword(String userId, String oldPassword, String newPassword) {
        User user = userDao.selectUserByUserId(userId);
        RetCode ret =null;
        if(user == null){
            ret = RetCode.USER_ID_NOT_FOUND;
        }
        else if(!user.getPassword().equals(oldPassword)){
            ret = RetCode.PASSWORD_ERROR;
        }
        else if(userDao.updateUserPassword(userId, newPassword).equals(0)){
            ret = RetCode.PASSWORD_CHANGE_FAILURE;
        }
        else{
            ret = RetCode.USER_SUCCESS;
        }
        return ret;
    }

    @Override
    public RetCode updateUserInfo(User user) {
        RetCode ret = this.checkUserFormat(user);
        if(ret != RetCode.USER_SUCCESS){
            return ret;
        }
        User userCheck;
        if((userCheck = userDao.selectUserByUserName(user.getUsername())) != null && !user.getUserId().equals(userCheck.getUserId())){
            ret = RetCode.USERNAME_EXISTS;
        }
        else if((userCheck = userDao.selectUserByPhoneNumber(user.getPhoneNumber())) != null && !user.getUserId().equals(userCheck.getUserId())){
            ret = RetCode.PHONE_NUM_EXISTS;
        }

        else if((userCheck = userDao.selectUserByEmail(user.getEmail())) != null && !user.getUserId().equals(userCheck.getUserId())){
            ret = RetCode.EMAIL_EXISTS;
        }
        else if(userDao.updateUserInfo(user) == 0){
            ret = RetCode.USER_INFO_CHANGE_FAILURE;
        }
        return ret;
    }
}
